package com.example.demo.utils;

import com.example.demo.constant.TaskConstant;
import com.example.demo.mapper.entity.Coupons;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author Lucy
 * @create 2024-02-19 23:14
 */
public class CouponsUtil {
    public static String generateCouponLabel(Coupons coupons) {
        switch (coupons.getCouponsType()) {
            case 1:
                return generateCashCouponLabel(coupons);
            case 2:
                return generateFullReductionCouponLabel(coupons);
            case 3:
                return generateDiscountCouponLabel(coupons);
            default:
                return generateRedemptionLabel(coupons);
        }
    }

    public static String generateCashCouponLabel(Coupons coupons) {
        return TaskConstant.CASH_LABEL.replace("value", Integer.toString(coupons.getValue().intValue()));
    }

    public static String generateFullReductionCouponLabel(Coupons coupons) {
        return TaskConstant.FULL_REDUCTION_LABEL
                .replace("value", Integer.toString(coupons.getValue().intValue()))
                .replace("minimum_amount", Integer.toString(coupons.getMinimumAmount().intValue()));
    }

    public static String generateDiscountCouponLabel(Coupons coupons) {
        double value = coupons.getValue(); // 获取小数值
        int digitsAfterDecimalPoint = String.valueOf(value).split("\\.")[1].length(); // 获取小数点后的位数
        int newValue = (int) (value * Math.pow(10, digitsAfterDecimalPoint)); // 将小数值乘以 10 的 n 次方
        return TaskConstant.DISCOUNT_LABEL.replace("value", Integer.toString(newValue));
    }

    public static String generateRedemptionLabel(Coupons coupons) {
        return TaskConstant.REDEMPTION_LABEL.replace("product_name", coupons.getProductName());
    }

    /**
     * 生成优惠券编号的方法
     *
     * @param advertiserId  广告号Id
     * @return couponNumber   优惠券编号
     */
    public static String generateCouponNumber(int advertiserId) {

        String couponNumber;
        // 使用UUID生成唯一的编号
        UUID uuid = UUID.randomUUID();
        // 结合广告主ID
        couponNumber = advertiserId + uuid.toString().replaceAll("-", "").substring(0, 8);
        return couponNumber;
        /*
        do {


        } while (redisUtil.hasKey(couponNumber)); // 检查是否重复
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 日期时间格式
        LocalDateTime endDate = LocalDateTime.parse(endDateStr, formatter);
        LocalDateTime now = LocalDateTime.now(); // 当前日期时间
        Duration duration = Duration.between(now, endDate);
        long seconds = duration.getSeconds();
        redisUtil.set(couponNumber, TaskConstant.COUPON_NUMBER_VALUE, seconds);// 将新编号添加到redis中
        */
    }
}
